<%@page import="org.apache.log4j.Logger"%>
<%@page import="com.cms.sync.manager.KpaySyncLogManager"%>
<%@page import="com.cms.sync.util.DesCrypt"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.util.LinkedHashMap"%>
<%@page import="java.util.HashMap"%>
<%@page import="javax.print.attribute.HashAttributeSet"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Map"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.File"%>
<%!
public class Config {

    private Map<String, Map<String, String>> sections;

    public Config(String str) {
    	// System.out.println("new Config: " + str.replace("\r", "\\r").replace("\n", "\\n"));
        sections = new LinkedHashMap<String, Map<String, String>>();
        String sec = ".";

        String[] lines = str.split("[\r\n]");
        for (String line : lines) {
            sec = parse(sec, line.trim());
        }
    }

    private String parse(String section, String line) {
        String sec = section;
        String l = line.trim();

        if (l.length() == 0 || l.startsWith("#"))
            return section;

        if (l.matches("\\[.*\\]")) {
            sec = l.replaceFirst("\\[(.*)\\]", "$1").trim();
            if (!section.equals(sec)) {
                sections.put(sec, new LinkedHashMap<String, String>());
            }
        } else if (l.matches(".*=.*")) {
            sec = section;
            int i = l.indexOf('=');
            String name = l.substring(0, i).trim();
            String value = l.substring(i + 1).trim();
            sections.get(sec).put(name, value);
        }
        return sec;
    }

    public String get(String section, String key, String defaultValue) {
        String value = get(section, key);
        if (value != null)
            return value;
        return defaultValue;
    }

    public String get(String section, String key)
    {
        if (section == null || section.length() == 0)
        {
            section = ".";
        }

        if (sections.containsKey(section))
        {
            if (sections.get(section).containsKey(key))
            {
                return sections.get(section).get(key);
            }
            else
            {
                if (!section.equals(".") && sections.get(".").containsKey(key))
                {
                    return sections.get(".").get(key);
                }
            }
        }
        return null;
    }

    public String get(String key)
    {
        if (sections.containsKey("."))
            return sections.get(".").get(key);
        return null;
    }

    public Map<String, String> getSection(String section) {
        if (sections.containsKey(section))
            return sections.get(section);
        return null;
    }
    
    public Map<String, Map<String, String>> getSections() {
        return sections;
    }
}
private String getRequestContent(HttpServletRequest request) {
	String string = "";
    if (request.getContentType() != null 
            && request.getContentType().startsWith("application/x-www-form-urlencoded"))
    {
        System.out.println("getInputStreamAsString:application/x-www-form-urlencoded");
        Map map = request.getParameterMap();
        Iterator iter = map.keySet().iterator();
        String s = "";
        while (iter.hasNext())
        {
            String key = (String) iter.next();
            String value = request.getParameter(key);
            s += value;
        }
        string = s.replace("\r", "").replace("\n", "").replace(" ", "+");
//      try
//      {
//          string = URLEncoder.encode(s.replace("\r", "").replace("\n", ""), "utf-8");
//      }
//      catch (Exception e)
//      {
//          e.printStackTrace();
//      }
    }
    else // text/xml
    {
        StringBuffer sb = new StringBuffer();
        try
        {
            InputStream in = request.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String s;
            while ((s = br.readLine()) != null){
                sb.append(s);
            }
            br.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        string = sb.toString();
    }
    
    return string;
}
%>
<%
// Config cfg = new Config(StringHelper.decrypt(getRequestContent(request)));
// String version = cfg.get("global", "version");
// System.out.println("version=" + version);
// String check = "";
DesCrypt crypt = new DesCrypt("event");
KpaySyncLogManager kpay = KpaySyncLogManager.getInstance();
String req = getRequestContent(request);
//System.out.println("request:["+req+"]");
String s = crypt.decrypt(req).trim();
if(s!=null&&s.length()>0){
	kpay.saveKpaySyncLog("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+s);
	
	Logger logger = Logger.getLogger("event");
	logger.info(s);
}
/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
File file = new File("/u02/ksdk/report/event_" + date.substring(0, 10) + ".txt");
//System.out.println(file.getPath());
if (!file.exists()) {
	file.createNewFile();
}
//System.out.println(file.getName());

FileOutputStream fos = new FileOutputStream(file, true);
fos.write((date + " -- " + s + "\r\n").getBytes());
fos.close();*/
response.reset();
PrintWriter pw = response.getWriter();
pw.write("0");
pw.flush();
pw.close(); 
if (true) return; 
%>
